package com.youmingxing.adpter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.youmingxing.R;
import com.youmingxing.activity.BaseActivity;
import com.youmingxing.activity.selfInfo.PersonalInfoActivity;
import com.youmingxing.bean.selfinfo.FansOrFocus;
import com.youmingxing.network.AllStarApi;
import com.youmingxing.network.BaseJsonHandler;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * Created by chihiro on 15/9/2.
 */
public class MyFocusAdapter extends BaseAdapter {
    private BaseActivity context;
    private List<FansOrFocus> mList = new ArrayList();
    private LayoutInflater inflater;

    public MyFocusAdapter(BaseActivity context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    public void setData(List<FansOrFocus> list) {
        if (list == null)
            return;
        if (mList == null) {
            mList = list;
        } else {
            mList.clear();
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    private class ViewHolder {
        TextView name;
        TextView motto;
        ImageView headImg;
        TextView btnFocus;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item_my_focus, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.user_name);
            holder.motto = (TextView) convertView.findViewById(R.id.user_motto);
            holder.headImg = (ImageView) convertView.findViewById(R.id.img_avatar);
            holder.btnFocus = (TextView) convertView.findViewById(R.id.button_focus);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        FansOrFocus myFocusUser = mList.get(position);
        holder.name.setText(myFocusUser.nickname);
        holder.motto.setText(myFocusUser.motto);
        Glide.with(context).load(myFocusUser.headimg)
                .placeholder(R.drawable.default_avatar).error(R.drawable.default_avatar)
                .bitmapTransform(new CropCircleTransformation(
                        context)).crossFade().into(holder.headImg);
        holder.btnFocus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FansOrFocus bean = mList.get(position);
                AllStarApi.focus(bean.id, 0, new BaseJsonHandler(context) {
                    @Override
                    public void onSuccessWithData(JSONObject response) {
                        int result = response.optJSONObject("data").optInt("result");
                        if (result == 1) {
                            Toast.makeText(context, "您已成功取消关注", Toast.LENGTH_SHORT).show();
                            mList.remove(position);
                            notifyDataSetChanged();
                        } else {
                            Toast.makeText(context, "出错了", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
        holder.headImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FansOrFocus bean = mList.get(position);
                Intent intent = new Intent(context, PersonalInfoActivity.class);
                intent.putExtra("u_id",bean.id);
                context.startActivity(intent);
            }
        });
        return convertView;
    }

}
